package pwd.allen.util;

import org.jxls.common.Context;
import org.jxls.transform.Transformer;
import org.jxls.util.JxlsHelper;

import java.io.*;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author lenovo
 * @create 2019-10-08 16:45
 **/
public class JxlsUtil {
    static{
        //添加自定义指令（可覆盖jxls原指令）
//        XlsCommentAreaBuilder.addCommandMapping("each", EachCommand.class);
//        XlsCommentAreaBuilder.addCommandMapping("merge", MergeCellsCommand.class);
    }
    /** jxls模版文件目录 */
    private final static String TEMPLATE_PATH = "jxlsTemplate";

    /**
     * 导出excel
     * @param is - excel文件流
     * @param os - 生成模版输出流
     * @param beans - 模版中填充的数据
     * @throws IOException
     */
    public static void exportExcel(InputStream is, OutputStream os, Map<String, Object> beans) throws IOException {
        Context context = new Context();
        if (beans != null) {
            for (String key : beans.keySet()) {
                context.putVar(key, beans.get(key));
            }
        }
        JxlsHelper jxlsHelper = JxlsHelper.getInstance();
        Transformer transformer  = jxlsHelper.createTransformer(is, os);
//        JexlExpressionEvaluator evaluator = (JexlExpressionEvaluator)transformer.getTransformationConfig().getExpressionEvaluator();
//        Map<String, Object> funcs = new HashMap<String, Object>();
//        funcs.put("jx", new JxlsUtil());    //添加自定义功能
//        JexlBuilder jexlBuilder = new JexlBuilder();
//        jexlBuilder.namespaces(funcs);
//        evaluator.setJexlEngine(jexlBuilder.create());
//        jxlsHelper.setUseFastFormulaProcessor(false);
        jxlsHelper.processTemplate(context, transformer);
    }

    /**
     * 导出excel
     * @param xlsPath excel文件
     * @param outPath 输出文件
     * @param beans 模版中填充的数据
     * @throws IOException
     * @throws FileNotFoundException
     */
    public static void exportExcel(String xlsPath, String outPath, Map<String, Object> beans) throws FileNotFoundException, IOException {
        exportExcel(new FileInputStream(xlsPath), new FileOutputStream(outPath), beans);
    }

    /**
     * 导出excel
     * @param xls excel文件
     * @param out 输出文件
     * @param beans 模版中填充的数据
     * @throws IOException
     * @throws FileNotFoundException
     */
    public static void exportExcel(File xls, File out, Map<String, Object> beans) throws FileNotFoundException, IOException {
        exportExcel(new FileInputStream(xls), new FileOutputStream(out), beans);
    }
    /**
     * 获取jxls模版文件
     */
    public static File getTemplate(String name) {
        String templatePath = JxlsUtil.class.getClassLoader().getResource(TEMPLATE_PATH).getPath();
        File template = new File(templatePath, name);
        if (template.exists()) {
            return template;
        }
        return null;
    }

    /**
     * 日期格式化
     * @param date
     * @param fmt
     * @return
     */
    public String dateFmt(Date date, String fmt) {
        if (date == null) {
            return null;
        }
        try {
            SimpleDateFormat dateFmt = new SimpleDateFormat(fmt);
            return dateFmt.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /** 返回第一个不为空的对象
     *
     * @param objs
     * @return
     */
    public Object defaultIfNull(Object... objs) {
        for (Object o : objs) {
            if (o != null)
                return o;
        }
        return null;
    }

    // if判断
    public Object ifelse(boolean b, Object o1, Object o2) {
        return b ? o1 : o2;
    }

    /**
     * 指定保留小数点后位数
     * @param bigDecimal
     * @param point
     * @return
     */
    public double parseDouble(BigDecimal bigDecimal, int point) {
        return bigDecimal.setScale(point, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}
